package com.jc.repository.configure;

import com.jc.idGen.gen.IdGenerator;
import com.jc.idGen.gen.ZkIdGenerator;
import com.jc.idGen.woker.Woker;
import com.jc.idGen.woker.ZkWoker;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by starmezhh on 16/7/21.
 */
@Configuration
@ConfigurationProperties(prefix = "com.jc.idGeneraotr")
public class IdGeneratorAutoconfiguration {

    String zookeeperUrl;

    String appName;

    public void setZookeeperUrl(String zookeeperUrl) {
        this.zookeeperUrl = zookeeperUrl;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    @Bean(initMethod = "init")
    public Woker getIdWoker(){
        ZkWoker idWorker = new ZkWoker(zookeeperUrl);
        if(appName==null||appName.equalsIgnoreCase(""))
            throw new RuntimeException("appName can not be null or empty!");
        idWorker.setAppName(this.appName);
        return idWorker;
    }

    @Bean(initMethod = "init")
    public IdGenerator getIdGenerator(Woker woker){
        if(woker==null)
            throw new RuntimeException("woker can not be null!");
        IdGenerator idGenerator = new ZkIdGenerator(woker);
        return idGenerator;
    }

}
